package nuc98x

import (
	"fmt"
	"golang.org/x/sys/unix"
	"openzbox.com/driver/conn/driver/driverreg"

	//"encoding/binary"
	"os"
)

//var f *os.File

type driverWatchDOG struct {
}

func (d *driverWatchDOG) String() string {
	return "nuc980-watchdog"
}

func (d *driverWatchDOG) Prerequisites() []string {
	//return []string{"nuc98x-gpio"}
	return nil
}

func (d *driverWatchDOG) After() []string {
	return nil
}
func smokeTest() error {
	const size = 4096  // 4kb
	const holeSize = 1 // Minimum DMA alignment.
	return nil
}
func (d *driverWatchDOG) Init() (bool, error) {

	return true, smokeTest()
}

func (d *driverWatchDOG) Close() error {
	// Stop DMA and PWM controllers.
	return nil
}

func init() {
	if isArm {
		// TODO(maruel): This is intense, wait to be sure it works.
		driverreg.MustRegister(&driverWDG)
	}
}

func InitWDT() (f *os.File, errx error) {
	//var err error
	f, err := os.Open("/dev/watchdog")
	if err != nil {
		if os.IsNotExist(err) {
			return nil, fmt.Errorf("/dev/watchdog is not configured.", rtcNumber)
		}
		return nil, fmt.Errorf(" watchdog error:  %v", err)
	}
	WDTKeepLive(f)
	return f, nil
}

func WDTKeepLive(f *os.File) (errx error) {
	return unix.IoctlWatchdogKeepalive(int(f.Fd()))
}

var driverWDG driverWatchDOG
